const $url = 'https://api.wuwei-tech.com/index.php/'

export default class Common {

  http(parames) {

    parames.method = parames.method || "get"
    parames.data = parames.data || {}
    let url = $url + parames.url
    let token = wx.getStorageSync('token') || ''
    let header = parames.header || {}
    header.token = token
    let that = this

    let tel = wx.getStorageSync('tel')
    wx.request({
      method: parames.method,
      url: url,
      header: header,
      data: parames.data,
      success: function (res) {
        if (res.data.status) {
          parames.success(res)
        } else {
          if (res.data.msg == '请登录!') {
            wx.reLaunch({
              url: `/pages/index/login/my_login/index?tab=${'id'}`
            })
          } else if (res.data.code == '-1'){
            wx.login({
              success: function (res) {
                wx.request({
                  url: $url + 'mp2/token/getToken',
                  data: {
                    code: res.code
                  },
                  success(res) {
                    wx.setStorageSync('token', res.data.data.token)
                  }
                })
              }
            })
          } else {
            wx.showModal({
              content: res.data.msg,
            })
          }
        }


      },
      fail: function (err) {
        console.log(err)
      }
    })

  }

  uploadFile(parames) {
    let url = $url + parames.url
    let token = wx.getStorageSync('token') || ''
    let header = parames.header || {}
    let formData = parames.formData || {}
    let filePath = parames.filePath
    header.token = token

    let that = this
    wx.uploadFile({
      url: url,
      filePath: parames.filePath,
      name: parames.name,
      header: header,
      formData: formData,
      success(res) {
        parames.success(res)
      }
    })
  }

  goUrl(e) {
    let url = e.url
    let method = e.method || "navigateTo";
    if (!url) {
      return;
    }
    //正常页面跳转
    wx[method]({
      url: url
    })
    //拨打电话
    if (url.indexOf('tel:') > -1) {
      wx.makePhoneCall({
        phoneNumber: url.split(':')[1],
      })
      return;
    }
    //网页跳转
    if (url.indexOf('http') > -1) {
      wx.navigateTo({
        url: `/pages/common/webview/webview?url=${url}`,
      })
      return;
    }
  }

  throttle(fn, gapTime) {
    if (gapTime == null || gapTime == undefined) {
      gapTime = 500
    }
    let _lastTime = null
    // 返回新的函数
    return function () {
      let _nowTime = +new Date()
      if (_nowTime - _lastTime > gapTime || !_lastTime) {
        fn.apply(this, arguments) //将this和参数传给原函数
        _lastTime = _nowTime
      }
    }
  }

  deepCopy(o) {
    let that = this;
    if (o instanceof Array) {
      var n = [];
      for (var i = 0; i < o.length; ++i) {
        n[i] = that.deepCopy(o[i]);
      }
      return n;
    } else if (o instanceof Function) {
      var n = new Function("return " + o.toString())();
      return n;
    } else if (o instanceof Object) {
      var n = {};
      for (var i in o) {
        n[i] = that.deepCopy(o[i]);
      }
      return n;
    } else {
      return o;
    }
  }
}